/*
 * GNationalCupGoalsDistributor.java
 * Created on 16 nov. 2008
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.impl.goals;

import java.math.BigDecimal;
import java.math.RoundingMode;

import com.hockeo.vhbase.conf.GRulesConstants;
import com.hockeo.vhbase.data.dao.club.GClub;
import com.hockeo.vhbase.data.dao.contest.GContestElement;
import com.hockeo.vhbase.data.dao.contest.GContestRound;
import com.hockeo.vhbase.data.helper.GContestUtils;
import com.hockeo.vhbase.error.GNotImplementedException;

/**
 * Calculates the number of goals to allocate for national cup rounds.
 *
 * @version $Id$
 * @author jjanke
 */
public class GNationalCupGoalsDistributor implements IGGoalsDistributor
{
  private BigDecimal[] d_adecGoals;

  public void initialize( GContestElement element )
  {
    GContestRound round = element.getContestRound();
    int nNumRemainingRounds = round.getContest().getContestRounds().size() + 1 - round.getRoundNum();

    d_adecGoals = getInitialGoals( nNumRemainingRounds );
  }

  /**
   * Returns how many goals are initially added to the goal account of each club for the
   * national cup contest. Index 0 contains no goals (<code>null</code>). The following
   * indexes of the array correspond to the goals which are given to each league category.
   * I.e. a team playing in the third league gets the amount of goals at index 3 of the
   * returned array.
   *
   * @param nNumRounds the number of rounds to be played in the national cup contest
   * @return an array containing the initial goal accounts at the index positions 1 to N
   *         (corresponding to the national league level)
   */
  private BigDecimal[] getInitialGoals( int nNumRounds )
  {
    BigDecimal[] adec = new BigDecimal[1 + GRulesConstants.MAX_NUM_LEAGUE_CATEGORIES];
    double dGoalsAvg = GContestUtils.getInitialGoalsKORound( 2, 3 ).doubleValue() * nNumRounds;

    // First league gets 100%, 2nd category 92,5%, 3rd 85% etc.
    //
    for ( int i = 1; i < adec.length; i++ )
      adec[i] = BigDecimal.valueOf( dGoalsAvg * ( 1.0 - ( ( i - 1 ) * 0.075 ) ) ).setScale( 1, RoundingMode.UP );

    return adec;
  }

  /**
   * For national cup contests, this method is not implemented and throws a
   * {@link GNotImplementedException}.
   *
   * @see com.hockeo.vhbase.data.impl.goals.IGGoalsDistributor#getGoals(int)
   */
  public BigDecimal getGoals( int nSeeding )
  {
    throw new GNotImplementedException();
  }

  public BigDecimal getGoals( GClub club )
  {
    return d_adecGoals[club.getLeague().getCategory()];
  }
}
